]> dgit.raspbian.org Git - gnome-shell.git/commitdiff
extensionBase: Add static lookupByURL() method
authorFlorian Müllner <fmuellner@gnome.org>
Mon, 10 Jul 2023 04:19:53 +0000 (06:19 +0200)
committerFlorian Müllner <fmuellner@gnome.org>
Sun, 30 Jul 2023 07:29:44 +0000 (10:29 +0300)
With convenience API like getSettings() now being provided by
the ExtensionObject subclass, extensions will need to access
their entry point more often.

Having to pass a pointer through the hierarchy can be annoying,
so add a static method that allows them to look it up:

```js
    const ext = Extension.lookupByURL(import.meta.url);
    this._settings = ext.getSettings();
```

Part-of: <https://gitlab.gnome.org/GNOME/gnome-shell/-/merge_requests/2838>

js/extensions/sharedInternals.js

index 89906d9da72ef65d5c1f7685b7e83fa6b759b860..8ec152332e25b7ca9223e88cee7d5b72a5b143df 100644 (file)
@@ -20,6 +20,32 @@ export function setExtensionManager(extensionManager) {
 export class ExtensionBase {
     #gettextDomain;
 
+    /**
+     * Look up an extension by URL (usually 'import.meta.url')
+     *
+     * @param {string} url - a file:// URL
+     */
+    static lookupByURL(url) {
+        if (!url.startsWith('file://'))
+            return null;
+
+        // Keep the last '/' from 'file://' to force an absolute path
+        let path = url.slice(6);
+
+        // Walk up the directory tree, looking for an extension with
+        // the same UUID as a directory name.
+        do {
+            path = GLib.path_get_dirname(path);
+
+            const dirName = GLib.path_get_basename(path);
+            const extension = _extensionManager.lookup(dirName);
+            if (extension !== undefined)
+                return extension.stateObj;
+        } while (path !== '/');
+
+        return null;
+    }
+
     /**
      * @param {object} metadata - metadata passed in when loading the extension
      */